package com.example.administrator.basedemos.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.administrator.basedemos.R;
import com.example.administrator.basedemos.adapter.RecommendFragmentAdapter;
import com.example.administrator.basedemos.utils.LogUtil;
import com.example.administrator.basedemos.utils.RecycleUtil;
import com.example.administrator.basedemos.view.ClassicsHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者     ZCL
 * 创建时间   2018/7/16 16:49
 * 描述	      ${TODO}
 * <p>
 * 更新者     $Author$
 * 更新时间   $Date$
 * 更新描述   ${TODO}
 */
public class ExchangeActivity extends BaseZcActivity implements BaseQuickAdapter.OnItemChildClickListener {
    private static final String TAG = "ExchangeActivity";
    @BindView(R.id.market_rank_l)
    TextView           mMarketRankL;
    @BindView(R.id.market_rank_m)
    TextView           mMarketRankM;
    @BindView(R.id.market_rank_r)
    TextView           mMarketRankR;
    @BindView(R.id.recyclerview_mm)
    RecyclerView       mRecyclerviewMm;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout mSmartRefresh;


    private RecommendFragmentAdapter mAdapter;
    private int                      mRankNumL;
    private int                      mRankNumM;
    private int                      mRankNumR;
    private ArrayList<String>        mListTabs;
    private ArrayList<String>        mList2;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_recommend;
    }

    @Override
    protected void initData() {
        setToolbarBackGroundColor(R.color.yellow);
        Bundle bundle = getIntent().getExtras();
        String title = (String) bundle.get("Title");
        setToolBarTitle(title);
        mRankNumL = 2;
        mRankNumM = 2;
        mRankNumR = 2;


        mListTabs = new ArrayList<>();
        mListTabs.add("钻石");
        mListTabs.add("VIP");
        mListTabs.add("转盘");
        mListTabs.add("奖品");

        mList2 = new ArrayList<>();
        mList2.add("xx");
        mList2.add("22xx2");
        mList2.add("xxx");
        mList2.add("xxx");

        RecycleUtil.setLayoutMangerVertical(mRecyclerviewMm, this);
        mAdapter = new RecommendFragmentAdapter(R.layout.item_recomend, mListTabs);


        //必须添加getparent否则不显示
        mAdapter.setEmptyView(R.layout.empty_view, (ViewGroup) mRecyclerviewMm.getParent());

        //Item子控件的点击事件
        //首先在adapter的convert方法里面通过viewHolder.addOnClickListener绑定一下的控件id
        mAdapter.setOnItemChildClickListener(this);
        mAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);


        mSmartRefresh.setRefreshHeader(new ClassicsHeader(this));
        mSmartRefresh.setHeaderHeight(60);

        mRecyclerviewMm.setAdapter(mAdapter);
    }


    @Override
    public void initListener() {
        super.initListener();

        mSmartRefresh.setOnRefreshListener(refreshLayout -> {
            refreshLayout.finishRefresh(2000, true);
            Toast.makeText(this, "哈哈哈下拉啊", Toast.LENGTH_SHORT).show();
        });
        mSmartRefresh.setOnLoadMoreListener(loadMoreLayout -> {
            //2秒加载成功没有数据
            Toast.makeText(this, "呵呵呵加载啊", Toast.LENGTH_SHORT).show();
            loadMoreLayout.finishLoadMore(2000, true, true);
        });
    }

    @OnClick({R.id.market_rank_l, R.id.market_rank_m, R.id.market_rank_r})
    public void OnClik(View v) {

        Drawable drawableL = null;
        Drawable drawableM = null;
        Drawable drawableR = null;
        switch (v.getId()) {
            case R.id.market_rank_l:
                setArrowChoice(drawableL, mRankNumL, 0);
                break;
            case R.id.market_rank_m:
                setArrowChoice(drawableM, mRankNumM, 1);
                break;
            case R.id.market_rank_r:
                setArrowChoice(drawableR, mRankNumR, 2);
                break;
            default:
                break;
        }
    }


    private void setArrowChoice(Drawable draw, Integer mRankNum, int type) {
        switch (mRankNum % 2) {
            case 0:
                draw = getResources().getDrawable(R.mipmap.down_b_arrow);
                break;
            case 1:
                draw = getResources().getDrawable(R.mipmap.up_b_arrow);
                break;
        }
        mRankNum++;
        draw.setBounds(0, 0, draw.getMinimumWidth(), draw.getMinimumHeight());
        switch (type) {
            case 0:
                mRankNumL = mRankNum;
                mMarketRankL.setCompoundDrawables(null, null, draw, null);
                break;
            case 1:
                mRankNumM = mRankNum;
                mMarketRankM.setCompoundDrawables(null, null, draw, null);
                break;
            case 2:
                mRankNumR = mRankNum;
                mMarketRankR.setCompoundDrawables(null, null, draw, null);
                break;
        }
    }


    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View v, int position) {
        LogUtil.util(TAG, "返回数据是:" + v.getId() + "======" + position);
        switch (v.getId()) {
            case R.id.item_exchange_ly:
                Toast.makeText(this, "点击前段" + mListTabs.get(position), Toast.LENGTH_SHORT).show();
                break;
          /*  case R.id.item_exchange_icon:
                Toast.makeText(getActivity(), "点击后段" + mList.get(position), Toast.LENGTH_SHORT).show();
                break;*/
        }
    }

}
